package xyz.doikki.dkplayer.activity;



import android.Manifest;
import android.os.Build;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

import xyz.doikki.dkplayer.R;
import xyz.doikki.dkplayer.fragment.TikTokListFragment;
import xyz.doikki.dkplayer.fragment.MineFragment;
import xyz.doikki.dkplayer.util.Tag;
import xyz.doikki.videoplayer.player.VideoView;

public class MainActivity extends BaseActivity<VideoView> implements NavigationBarView.OnItemSelectedListener {
    public static int mCurrentIndex = 0;
    private List<Fragment> mFragments = new ArrayList<Fragment>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean enableBack() {
        return false;
    }

    @Override
    protected void initView() {
        super.initView();
//        setStatusBarTransparent();
//        getWindow().setStatusBarColor(Color.WHITE);
//        copyAssetsFile();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 10000);
        }
        setTitle("首页");

        BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
        bottomNavigationView.setOnItemSelectedListener(this);
        mFragments.add(new TikTokListFragment());
        mFragments.add(new MineFragment());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.layout_content, mFragments.get(0))
                .commitAllowingStateLoss();
        mCurrentIndex = 0;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int index;
        int itemId = menuItem.getItemId();
        switch (itemId) {
            case R.id.tab_home:
                index = 0;
                break;
            case R.id.tab_mine:
                index = 1;
                break;
            default:
                index = 0;
                break;
        }

        if (mCurrentIndex != index) {
            //切换tab，释放正在播放的播放器
            if (mCurrentIndex == 1) {
                getVideoViewManager().releaseByTag(Tag.LIST);
                getVideoViewManager().releaseByTag(Tag.SEAMLESS, false); //注意不能移除
            }
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Fragment fragment = mFragments.get(index);
            Fragment curFragment = mFragments.get(mCurrentIndex);
            if (fragment.isAdded()) {
                transaction.hide(curFragment).show(fragment);
            } else {
                transaction.add(R.id.layout_content, fragment).hide(curFragment);
            }
            transaction.commitAllowingStateLoss();
            mCurrentIndex = index;
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        if (getVideoViewManager().onBackPress(Tag.LIST)) {
            return;
        }
        if (getVideoViewManager().onBackPress(Tag.SEAMLESS)) {
            return;
        }
        super.onBackPressed();
    }

//    private boolean copyAssetsFile() {
//        File outFile = new File(getExternalCacheDir(), "test.mp4");
//        if (!outFile.getParentFile().exists()) {
//            outFile.getParentFile().mkdirs();
//        }
//        InputStream inputStream = null;
//        OutputStream out = null;
//
//        try {
//            inputStream = getAssets().open("test.mp4");
//            out = new FileOutputStream(outFile);
//
//            byte[] buffer = new byte[1024];
//            int read = inputStream.read(buffer);
//            while (read != -1) {
//                out.write(buffer, 0, read);
//                read = inputStream.read(buffer);
//            }
//        } catch (IOException e) {
//            return false;
//        } finally {
//            if (inputStream != null) {
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                }
//            }
//            if (out != null) {
//                try {
//                    out.flush();
//                    out.close();
//                } catch (IOException e) {
//                }
//            }
//        }
//        return true;
//    }

}